﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        string st1 = "";        
        char c;
        int x=0, y=0;
        public Form1()
        {
            InitializeComponent();
            Buttons();
        }
        private void Buttons()
        {
            Numpanel.AutoSize = true;
            Varpan.AutoSize = true;
            for (int j = 0; j < 4; j++)
                for (int i= 0; i < 3; i++)
                {   
                    string caption = "789456123 0 ";

                    if (!(caption[j * 3 + i] == ' '))                   
                    {
                        Button Btn = new Button();
                        Btn.Parent = Numpanel;
                        Btn.Name = Convert.ToString(caption[j * 3 + i]);
                        Btn.Text = Convert.ToString(caption[j * 3 + i]);
                        Btn.Click += new System.EventHandler(NumberClick);                        
                        Btn.Width = 30;
                        Btn.Height = 30;
                        Btn.Left = i * 30;
                        Btn.Top = 30 * j;
                       
                        
                    }
                }

            for (int i = 0; i < 5; i++)
            {
                string caption = "+-*/=";
                Button Btn = new Button();
                Btn.Parent = Varpan;
                Btn.Name = Convert.ToString(caption[i]);  
                Btn.Text = Convert.ToString(caption[i]);                
                Btn.Width = 30;
                Btn.Height = 30;               
                Btn.Top = 30 * i;               
                if (!(caption[i] == '='))
                {
                    Btn.Click += new System.EventHandler(VarClick);
                }
                else
                {
                    Btn.Click += new System.EventHandler(MakesClick);
                }
            }
        }
        private void NumberClick(object sender, EventArgs e)
        {
            st1 = st1+((System.Windows.Forms.Control)(sender)).Text;            
            AddW.Text = st1; 
        }
        private void VarClick(object sender, EventArgs e)
        {
            x = Convert.ToInt32(AddW.Text);
            c = Convert.ToChar(((System.Windows.Forms.Control)(sender)).Text);
            st1 = "";
            AddW.Text = "";

        }
        static float calculate(int a, int b, char f)
        {
            switch (f)
            {
                case '*': return a * b;

                case '+': return a + b;

                case '-': return a - b;

                case '/': if (b == 0)
                        throw new System.DivideByZeroException();
                    return a / b;
                default: throw new System.ArgumentException("Данная ф-ия не поддерживается", "действие");

            }
        }
        private void MakesClick(object sender, EventArgs e)
        {
            y = Convert.ToInt32(st1);
            float res = 0;
            res = calculate(x, y, c);
            AddW.Text = res.ToString();
            x = Convert.ToInt32(res);

        }
    }
}
